{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [JavaScript Number 对象](https://www.runoob.com/js/js-obj-number.html)\n",
    "JavaScript 只有一种数字类型。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 所有 JavaScript 数字均为 64 位\n",
    "JavaScript 不是类型语言。与许多其他编程语言不同，JavaScript 不定义不同类型的数字，比如整数、短、长、浮点等等。\n",
    "\n",
    "在JavaScript中，数字不分为整数类型和浮点型类型，所有的数字都是由 `浮点型类型`。JavaScript 采用 IEEE754 标准定义的 64 位浮点格式表示数字，它能表示`最大值为 ±1.7976931348623157e+308`，`最小值为 ±5e-324`。\n",
    "\n",
    "|值 (aka Fraction/Mantissa)|\t指数|\tSign|\n",
    "|:---|:---|:---|\n",
    "|52 bits (0 - 51)| \t11 bits (50 - 62)|\t1 bit (63)|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 精度\n",
    "整数（不使用小数点或指数计数法）最多为 15 位。\n",
    "\n",
    "小数的最大位数是 17，但是浮点运算并不总是 100% 准确。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 八进制和十六进制\n",
    "如果前缀为 0，则 JavaScript 会把数值常量解释为八进制数，如果前缀为 0 和 \"x\"，则解释为十六进制数。\n",
    "\n",
    "**绝不要在数字前面写零，除非您需要进行八进制转换**。\n",
    "\n",
    "默认情况下，JavaScript 数字为十进制显示。\n",
    "\n",
    "但是你可以使用 toString() 方法 输出16进制、8进制、2进制。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "var myNumber = 128;\n",
    "var a = myNumber.toString(16);   // 返回 80\n",
    "var b = myNumber.toString(8);    // 返回 200\n",
    "var c = myNumber.toString(2);    // 返回 10000000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "80\n",
      "200\n",
      "10000000\n",
      "a8\n"
     ]
    }
   ],
   "source": [
    "console.log(a);\n",
    "console.log(b);\n",
    "console.log(c);\n",
    "var d = myNumber.toString(12);\n",
    "console.log(d);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 无穷大（Infinity）\n",
    "当数字运算结果超过了JavaScript所能表示的数字上限（溢出），结果为一个特殊的无穷大（infinity）值，在JavaScript中以Infinity表示。同样地，当负数的值超过了JavaScript所能表示的负数范围，结果为负无穷大，在JavaScript中以-Infinity表示。无穷大值的行为特性和我们所期望的是一致的：基于它们的加、减、乘和除运算结果还是无穷大（当然还保留它们的正负号）。\n",
    "\n",
    "**无穷大是个数字**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "16\n",
      "256\n",
      "65536\n",
      "4294967296\n",
      "18446744073709552000\n",
      "3.402823669209385e+38\n",
      "1.157920892373162e+77\n",
      "1.3407807929942597e+154\n",
      "Infinity\n"
     ]
    }
   ],
   "source": [
    "myNumber=2;\n",
    "while (myNumber!=Infinity){\n",
    "    myNumber=myNumber*myNumber; // 重复计算直到 myNumber 等于 Infinity\n",
    "    console.log(myNumber);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 补充：JavsScript 支持除以零!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除以0也产生了无限:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "var x = 2/0;\n",
    "var y = -2/0;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Infinity\n",
      "-Infinity\n"
     ]
    }
   ],
   "source": [
    "console.log(x);\n",
    "console.log(y);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NaN - 非数字值\n",
    "NaN 属性是代表非数字值的特殊值。该属性用于指示某个值不是数字。可以把 Number 对象设置为该值，来指示其不是数字值。\n",
    "\n",
    "你可以使用 isNaN() 全局函数来判断一个值是否是 NaN 值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true\n",
      "false\n"
     ]
    }
   ],
   "source": [
    "var x = 1000 / \"Apple\";\n",
    "console.log(isNaN(x)); // 返回 true\n",
    "\n",
    "var y = 100 / \"1000\";\n",
    "console.log(isNaN(y)); // 返回 false"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Javascript (Node.js)",
   "language": "javascript",
   "name": "javascript"
  },
  "language_info": {
   "file_extension": ".js",
   "mimetype": "application/javascript",
   "name": "javascript",
   "version": "13.13.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
